﻿using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Net对文件操作 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        xxfile();
    }
    //向文本文件写入文本的简单方法;文件为当前目录下的TestFile.Xml



    public void aa()
    {
        string FILE_NAME;
        FILE_NAME = Server.MapPath("TestFile.txt");
        if (File.Exists(FILE_NAME))//如果文件名在的话
        {
            Console.WriteLine("{0} already exists.", FILE_NAME);
            return;
        }
        StreamWriter sr = File.CreateText(FILE_NAME);
        sr.WriteLine("This is my file.");
        sr.WriteLine("I can write ints {0} or floats {1}, and so on.",
            1, 4.2);
        sr.Close();
    }


    protected void cc(object sender, EventArgs e)//对文件的操作
    {
        string file = Server.MapPath("TestFile.txt").ToString();

        if (File.Exists(file))//文件是不是存在
        {
            Response.Write("文件在啊");
            StreamWriter sr = new StreamWriter(file);//向文件里写入文件
            sr.Write(tt.Text);
            sr.Close();


        }
        else
        {
            StreamWriter sw = File.CreateText(file);//创建文件;
            sw.WriteLine(tt.Text);
            sw.Close();
        }

        //读取文本文件内容




    }

    public void xxfile()//读取文件的内容
    {
        string file = Server.MapPath("TestFile.Xml").ToString();
        try
        {
            if (File.Exists(file))//看看文件是不是存在
            {
                StreamReader read = new StreamReader(file);//读取指定文件
                if (read.ReadLine() != null)
                {
                    Response.Write(read.ReadLine().ToString());

                }
                read.Close();
            }
            else//如果文件不在的话.
            {
                StreamWriter sw = File.CreateText(file);//创建文件
                sw.Write(tt.Text);
                sw.Close();

            }

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }


}
